package database.dao.Impl;

import database.dao.BusinessDao;
import entity.Business;
import entity.Customer;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName BusinessDaoimpl
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/17 15:00
 */
public class BusinessDaoImpl extends BaseDao implements BusinessDao {

    @Override
    public Integer addBusiness(Business business) {
        String psql="insert into business(name,password,telephone,address,sex)" +
                "values(?,?,?,?,?)";
        Object[] values={business.getName(),business.getPassword(),business.getTelephone(),business.getAddress(),business.getSex()};
        super.executeUpdate(psql,values);
        return getAutoId("Business","business_id");
    }

    @Override
    public Business selectBusiness(String business_id) {
        String psql="SELECT * FROM business WHERE business_id= ? ";
        Object[] value={ business_id };
        ResultSet resultSet= super.executeQuerySQL(psql,value);
        Business business =new Business();
        try {
            if(resultSet.next()){
                business.setName(resultSet.getString("name"));
                business.setBusiness_id(resultSet.getString("business_id"));
                business.setTelephone(resultSet.getString("telephone"));
                business.setAddress(resultSet.getString("address"));
                business.setSex(resultSet.getString("Sex"));
                business.setPassword(resultSet.getString("Password"));
            }else{
                System.out.println("未查询到");
                return null;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return business;
    }

    @Override
    public void updateBusiness(Business business) {
        String psql="update Business name set=?,password =?,telephone =?,address =?,sex=? where customer_id=?";
        Object[] values={business.getName(),business.getPassword(),business.getTelephone(),business.getAddress(),business.getSex(),business.getBusiness_id()};
        super.executeUpdate(psql,values);
    }
}
